<template>
  <!-- 主车定损-->
  <view class="normal-login-container">

    <view class="item-wrap" style="margin-bottom: 0">
      <view class="lineBottom10">
        <uni-section title="主车定损" type="line" />
      </view>

      <view style="margin: 8rpx 20rpx 8rpx 20rpx">
        <uni-forms ref="ruleForm" :rules="rules" :label-position="boole ? 'top' : 'left'" :model="ruleForm" labelWidth="auto">

          <uni-forms-item label="主车定损金额:" name="vehicleAssessmentPrice">
            <text class="vehicleAssessmentPrice" />
            <uni-easyinput v-if="boole" v-model="ruleForm.vehicleAssessmentPrice"
                           @clear="clear('vehicleAssessmentPrice')"
                           @blur="blurAmount('vehicleAssessmentPrice')"
              placeholder="请输主车定损金额" />
            <text v-else> {{ ruleForm.vehicleAssessmentPrice || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="人伤金额:" name="persionAssessmentPrice">
            <text class="persionAssessmentPrice" />
            <uni-easyinput v-if="boole" v-model="ruleForm.persionAssessmentPrice"
                           @clear="clear('persionAssessmentPrice')"
                           @blur="blurAmount('persionAssessmentPrice')"
              placeholder="请输入人伤金额" />
            <text v-else> {{ ruleForm.persionAssessmentPrice || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="其他金额:" name="otherAssessmentPrice">
            <text class="otherAssessmentPrice" />
            <uni-easyinput v-if="boole" v-model="ruleForm.otherAssessmentPrice"
                           @clear="clear('otherAssessmentPrice')"
                           @blur="blurAmount('otherAssessmentPrice')"
              placeholder="请输入其他金额" />
            <text v-else> {{ ruleForm.otherAssessmentPrice || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="定损总计:">
            <uni-easyinput v-if="boole" v-model="ruleForm.assessmentTotalPrice" disabled @blur="blurAmount('assessmentTotalPrice')"
              placeholder="请输入定损总计" />
            <text v-else> {{ ruleForm.assessmentTotalPrice || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="签约维修厂/维修厂:" name="maintenanceShopId" :required="boole">
            <text class="maintenanceShopId" />
            <zxz-uni-data-select v-if="boole" v-model="ruleForm.maintenanceShopId" :localdata="options" dataKey="supplierName"
              dataValue="supplierId" placeholder="签约维修厂/维修厂"
                                 @change="handleMaintenanceShopId"
                                 :clear="true" filterable />
            <text v-else>{{ ruleForm.maintenanceShop || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="事故照片:">
            <image-common :disabled="!boole" :up-type="2" v-model="ruleForm.accidentPhotos"/>
          </uni-forms-item>

          <uni-forms-item label="定损单:">
            <image-common :disabled="!boole" :up-type="2" v-model="ruleForm.lossAssessmentOss"/>
          </uni-forms-item>

        </uni-forms>
      </view>
    </view>
  </view>
</template>

<script>
import {toNumberSub,toNumberAdd, toNumberRide, toNumberExcept} from '@/pages/quota/libs/utils.js';

export default {
  name: "masterCar", //主车定损
  components: {},
  props: {
    value: {
      type: Object,
      default: {},
    },
    boole: {
      type: Boolean,
      default: true,
    },
    options: {
      type: Array,
      default: [],
    },
    depreciationList: {
      type: Array,
      default: [],
    },
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", { ...target, [key]: value });
            return true;
          },
        });
        return proxy;
      },
    },
  },
  data() {
    return {
      rules: {
        maintenanceShopId: {
          rules: [
            {
              required: true,
              errorMessage: "签约维修厂/维修厂",
              validateTrigger: "bind",
            },
          ],
        },
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    handleMaintenanceShopId(val){
      if(val){
        this.value.maintenanceShopId = this.ruleForm.maintenanceShopId = val.supplierId
        this.value.maintenanceShop = this.ruleForm.maintenanceShop = val.supplierName
      }
    },

    blurAmount(str) {
      if (this.ruleForm[str]) {
        let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
        if (!msg.test(this.ruleForm[str])) {
          this.ruleForm[str] = "";
        } else {
          this.ruleForm[str] = this.ruleForm[str]
            .toString()
            .match(/^\d+(?:\.\d{0,2})?/)[0];
          if (
            str === "vehicleAssessmentPrice" &&
            this.value.depreciationPlanId
          ) {
            let obj = this.depreciationList.filter(
              (i) => i.id === this.value.depreciationPlanId
            )[0];
            let a = toNumberSub(
              Number(this.ruleForm.vehicleAssessmentPrice || 0),
              Number(obj.deductionAmount || 0)
            );
            let b = toNumberRide(a, Number(obj.scale || 0));
            let num = toNumberExcept(b, 100);
            this.$set(this.value, "depreciationPrice", num > 0 ? num : 0);
            this.$set(this.ruleForm, "depreciationPrice", num > 0 ? num : 0);
          }
        }
      }
      this.sumPrice()
    },

    clear(str){
      this.value[str] = this.ruleForm[str] = ''
      this.sumPrice()
    },

    //定损总计
    sumPrice() {
      let num = 0;
      if (this.ruleForm.maintenanceType === "1") {
        num = toNumberAdd(
          toNumberAdd(
            Number(this.ruleForm.vehicleAssessmentPrice),
            Number(this.ruleForm.residualValue)
          ),
          Number(this.ruleForm.carOther)
        );
      }
      if (this.ruleForm.maintenanceType === "3") {
        num = toNumberAdd(
          toNumberAdd(
            Number(this.ruleForm.vehicleAssessmentPrice),
            Number(this.ruleForm.persionAssessmentPrice)
          ),
          Number(this.ruleForm.otherAssessmentPrice)
        );
      }

      this.ruleForm.assessmentTotalPrice = this.value.assessmentTotalPrice = num
        .toString()
        .match(/^\d+(?:\.\d{0,2})?/)[0];
    },

    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm
          .validate()
          .then((res) => {
            resolve();
          })
          .catch((err) => {
            let key = ".flex-col" + ">>>" + "." + err[0].key;
            reject(key);
          });
      });
    },
  },
};
</script>

<style scoped lang=""></style>


